package thread;

import java.util.Scanner;

public class testDemo16 {
    //内存可见性实例讲解
    public  volatile static int flag=0; //加上volatile实现代码优化的消除
    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            while (flag==0){
                //不输出任何；
                /*try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }*/
            }
                System.out.println("flag的值进行了改变");

        });
        Thread t2=new Thread(()->{
            System.out.println("输入一个flag的值");
            Scanner scanner=new Scanner(System.in);
            flag=scanner.nextInt();
        });
        //起启动线程
        t1.start();
        t2.start();
    }
}
